/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx.menu.handlers;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import rpg.RPG;
import rpg.ui.components.Component;
import rpg.ui.components.ComponentState;
import rpg.ui.components.ComponentTextfield;
import rpg.ui.components.IClickableComponent;
import rpg.ui.components.IDraggableComponent;
import rpg.ui.components.ISubcomponentHolder;
import rpg.gfx.dialogs.Dialog;
import rpg.gfx.dialogs.DialogFactory;

/**
 *
 * @author rothens
 */
public class HoverMenuHandler implements IMenuHandler {

    public int active;
    ArrayList<Component> components;

    @Override
    public boolean handleTopEvents() {
        Dialog d = DialogFactory.getInstance().activeDialog();
        MouseEvent pos = RPG.mouselistener.lastPosition;
        int mouseX = (pos == null) ? -1 : pos.getX();
        int mouseY = (pos == null) ? -1 : pos.getY();

        if (RPG.keylistener.isKeyDown2(KeyEvent.VK_TAB) || RPG.keylistener.isKeyDown2(KeyEvent.VK_DOWN)) {
            active = getNextFocusable(components, active);
        }
        if (RPG.keylistener.isKeyDown2(KeyEvent.VK_UP)) {
            active = getPrevFocusable(components, active);
        }

        //components.get(active).setState(ComponentState.SELECTED);
        if (d != null && d.isMouseOver(mouseX, mouseY)) {
            d.handleEvents();
        } else {
            for (int i = 0; i < components.size(); i++) {
                Component c = components.get(i);

                if (c.isMouseOver(mouseX, mouseY)) {
                    c.setState(ComponentState.HOVER);
                    if (c instanceof ISubcomponentHolder) {
                        ISubcomponentHolder ish = (ISubcomponentHolder) c;
                        if (ish.getSubComponents() != null && ish.isInsideSubcomponentArea(mouseX - c.getX(), mouseY - c.getY())) {
                            handleSubEvents(ish.getSubComponents(), ish, mouseX - c.getX(), mouseY - c.getY());
                        }
                    }
                    if (RPG.mouselistener.isMousePressed()) {
                        if (c instanceof IClickableComponent) {
                            if (((IClickableComponent) c).onPress(mouseX, mouseY)) {
                                return true;
                            }
                        }
                    }
                    if (RPG.mouselistener.isMouseReleased()) {
                        if (c instanceof IClickableComponent) {
                            if (((IClickableComponent) c).onRelease(mouseX, mouseY)) {
                                return true;
                            }
                        } else if (c.isMouseOver(mouseX, mouseY)) {
                            c.setState(ComponentState.SELECTED);
                            active = i;
                        }
                    }

                } else if (active == i) {
                    c.setState(ComponentState.SELECTED);
                } else {
                    c.setState(ComponentState.INACTIVE);
                }

                if (c instanceof IDraggableComponent) {
                    IDraggableComponent idc = (IDraggableComponent) c;
                    if (c.isMouseOver(mouseX, mouseY)) {
                        if (RPG.mouselistener.isMousePressed()) {
                            idc.onStartDrag(mouseX, mouseY);
                        }
                    }
                    if (idc.isDragged()) {
                        idc.whileDrag(mouseX, mouseY);
                        if (RPG.mouselistener.isMouseReleased()) {
                            idc.onEndDrag(mouseX, mouseY);
                        }
                    }
                }
                //c.redraw();
            }
        }



        if (RPG.keylistener.isKeyDown2(KeyEvent.VK_ENTER)) {
            if (d == null) {
                onEnter(components, active);
                return true;
            } else {
                d.handleEvents();
                return true;
            }

        }

        Component de = components.get(active);

        if (de instanceof ComponentTextfield && d == null) {
            ((ComponentTextfield) de).append(RPG.keylistener.getAndClearCache());
            if (RPG.keylistener.isKeyDown2(KeyEvent.VK_BACK_SPACE)) {
                ((ComponentTextfield) de).backspace();
            }
        } else {
            RPG.keylistener.clearCache();
        }

        return false;

    }

    @Override
    public int getNextFocusable(ArrayList<Component> components, int active) {
        for (int i = active + 1; i < active + components.size(); i++) {
            if (components.get(i % components.size()).isFocusable()) {
                return i % components.size();
            }
        }
        return active;
    }

    @Override
    public int getPrevFocusable(ArrayList<Component> components, int active) {
        int act = active;
        for (int i = active + 1; i < active + components.size(); i++) {
            if (components.get(i % components.size()).isFocusable()) {
                act = i % components.size();
            }
        }
        return act;
    }

    @Override
    public void onEnter(ArrayList<Component> components, int active) {
        components.get(active).action();
    }

    @Override
    public void setComponents(ArrayList<Component> components) {
        this.components = components;
    }

    @Override
    public boolean handleSubEvents(ArrayList<Component> components, ISubcomponentHolder parent, int x, int y) {
        //System.out.println(RPG.mouselistener.isMouseReleased());
        int mouseX = x + parent.getXDiff();
        int mouseY = y + parent.getYDiff();

        if (parent.getActive() > -1) {
            components.get(parent.getActive()).setState(ComponentState.SELECTED);
        }

        for (int i = 0; i < components.size(); i++) {
            Component c = components.get(i);

            if (c.isMouseOver(mouseX, mouseY)) {
                c.setState(ComponentState.HOVER);
                if (RPG.mouselistener.isMousePressed()) {
                    if (c instanceof IClickableComponent) {
                        if (((IClickableComponent) c).onPress(mouseX, mouseY)) {
                            return true;
                        }
                    }
                }
                if (RPG.mouselistener.isMouseReleased()) {
                    if (c instanceof IClickableComponent) {
                        if (((IClickableComponent) c).onRelease(mouseX, mouseY)) {
                            return true;
                        }
                    } else if (c.isMouseOver(mouseX, mouseY)) {
                        c.setState(ComponentState.SELECTED);
                        parent.setActive(i);
                    }
                }
            } else if (parent.getActive() == i) {
                c.setState(ComponentState.SELECTED);
            } else {
                c.setState(ComponentState.INACTIVE);
            }

            if (c instanceof IDraggableComponent) {
                IDraggableComponent idc = (IDraggableComponent) c;
                if (c.isMouseOver(mouseX, mouseY)) {
                    if (RPG.mouselistener.isMousePressed()) {
                        idc.onStartDrag(mouseX, mouseY);
                    }
                }
                if (idc.isDragged()) {
                    idc.whileDrag(mouseX, mouseY);
                    if (RPG.mouselistener.isMouseReleased()) {
                        idc.onEndDrag(mouseX, mouseY);
                    }
                }
            }
            //c.redraw();
        }



        if (parent.getActive() > -1) {
            Component de = components.get(parent.getActive());

            if (de instanceof ComponentTextfield) {
                ((ComponentTextfield) de).append(RPG.keylistener.getAndClearCache());
                if (RPG.keylistener.isKeyDown2(KeyEvent.VK_BACK_SPACE)) {
                    ((ComponentTextfield) de).backspace();
                }
            } else {
                RPG.keylistener.clearCache();
            }
        }
        return false;
    }
}